home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / UDPSUBR.C < prev    next >
C/C++ Source or Header  |  1990-03-13  |  1KB  |  53 lines

  1. /* Convert UDP header in mbuf to internal structure */
  2.  
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "internet.h"
  6. #include "netuser.h"
  7. #include "udp.h"
  8.  
  9. /* Convert UDP header in internal format to an mbuf in external format */
  10. struct mbuf *
  11. htonudp(udp,data,ph)
  12. struct udp *udp;
  13. struct mbuf *data;
  14. struct pseudo_header *ph;
  15. {
  16.     struct mbuf *bp;
  17.     register char *cp;
  18.     int16 checksum;
  19.  
  20.     /* Allocate UDP protocol header and fill it in */
  21.     if((bp = pushdown(data,UDPHDR)) == NULLBUF)
  22.         return NULLBUF;
  23.  
  24.     cp = bp->data;
  25.     cp = put16(cp,udp->source);    /* Source port */
  26.     cp = put16(cp,udp->dest);    /* Destination port */
  27.     cp = put16(cp,udp->length);    /* Length */
  28.     *cp++ = 0;            /* Clear checksum */
  29.     *cp-- = 0;
  30.  
  31.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  32.      * the spec requires us to change zeros into ones to distinguish an
  33.      * all-zero checksum from no checksum at all
  34.      */
  35.     if((checksum = cksum(ph,bp,ph->length)) == 0)
  36.         checksum = ~0;
  37.     put16(cp,checksum);
  38.     return bp;
  39. }
  40.  
  41. ntohudp(udp,bpp)
  42. struct udp *udp;
  43. struct mbuf **bpp;
  44. {
  45.     char udphead[8];
  46.  
  47.     pullup(bpp,udphead,8);
  48.     udp->source = get16(udphead);
  49.     udp->dest = get16(udphead + 2);
  50.     udp->length = get16(udphead + 4);
  51.     udp->checksum = get16(udphead + 6);
  52. }
  53.